package net.melon.page.inst.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.esj.basic.core.view.tools.ActionContext;
import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.service.impl.DefaultServiceImpl;
import net.esj.basic.utils.Pagination;
import net.esj.basic.utils.Validators;
import net.melon.article.pojo.Article;
import net.melon.page.pojo.Module;
import net.melon.page.pojo.ModuleLink;
import net.melon.page.pojo.ModuleLinkType;
import net.melon.page.pojo.ModuleLinkType.ModuleLinkTypeType;
import net.melon.page.pojo.dto.PageDto;
import net.melon.product.pojo.Product;
import net.melon.uploader.Uploader;
import net.melon.uploader.pojo.FileUpload;
import net.melon.uploader.service.impl.DateSplitPathRule;
import net.melon.uploader.service.impl.RandomFileNameRule;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("moduleService")
public class ModuleServiceImpl extends DefaultServiceImpl implements ModuleService {

	@Override
	public Pagination getModuleLinkTypePage(Pagination pagination,
			int moduleId) {
		pagination.getQueryBuilder().eq("module.id", moduleId);
		return queryPageByQueryBuilder(pagination);
	}

	@Override
	public Pagination getModuleLinkPage(Pagination pagination,
			int moduleLinkTypeId) {
		pagination.getQueryBuilder().eq("type.id", moduleLinkTypeId);
		return queryPageByQueryBuilder(pagination);
	}

	@Override
	public void addModuleLink(ModuleLink entity) {
		ModuleLinkType type = get(entity.getType().getId(), ModuleLinkType.class);
		if(type.getType() == ModuleLinkTypeType.PICTURE.getType()){//图片
			try {
				Uploader uploader = new Uploader(new DateSplitPathRule(), new RandomFileNameRule(), ModuleLink.MODULE_LINK_ICON);
				uploader.setAllowFiles(Uploader.ALLOW_FILE_IMAGES);
				List<FileUpload> list = uploader.upload((HttpServletRequest)ActionContext.getRequest());
				if(!list.isEmpty()){
					FileUpload up = list.get(0);
					entity.setValue(up.getUrl());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		save(entity);
	}

	@Transactional
	@Override
	public void deleteModuleLinkType(Integer id) {
		ModuleLinkType type = get(id, ModuleLinkType.class);
		if(type!=null){
			QueryBuilder<ModuleLink> qb = createQueryBuilder(ModuleLink.class);
			qb.eq("type", type);
			List<ModuleLink> list = queryListByQueryBuilder(qb);
			if(!Validators.isEmpty(list)){
				deleteAll(list);
			}
			delete(type);
		}
	}

	@Transactional
	@Override
	public void deleteModule(Integer id) {
		Module module = get(id, Module.class);
		if(module!=null){
			QueryBuilder<ModuleLinkType> qb = createQueryBuilder(ModuleLinkType.class);
			qb.eq("module", module);
			List<ModuleLinkType> moduleLinkTypelist = queryListByQueryBuilder(qb);
			if(!Validators.isEmpty(moduleLinkTypelist)){
				QueryBuilder<ModuleLink> qb2 = createQueryBuilder(ModuleLink.class);
				qb2.in("type", moduleLinkTypelist);
				List<ModuleLink> moduleLinkList = queryListByQueryBuilder(qb2);
				if(!Validators.isEmpty(moduleLinkList)){
					deleteAll(moduleLinkList);
				}
				deleteAll(moduleLinkTypelist);
			}
			delete(module);
		}
	}

	@Override
	public void updateModuleLink(ModuleLink entity) {
		ModuleLinkType type = get(entity.getType().getId(), ModuleLinkType.class);
		if(type.getType() == ModuleLinkTypeType.PICTURE.getType()){//图片
			try {
				Uploader uploader = new Uploader(new DateSplitPathRule(), new RandomFileNameRule(), ModuleLink.MODULE_LINK_ICON);
				uploader.setAllowFiles(Uploader.ALLOW_FILE_IMAGES);
				List<FileUpload> list = uploader.upload((HttpServletRequest)ActionContext.getRequest());
				if(!list.isEmpty()){
					FileUpload up = list.get(0);
					entity.setValue(up.getUrl());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		update(entity);
	}

	@Override
	public ModuleLink getModuleLinkWithType(Integer id) {
		QueryBuilder<ModuleLink> qb = createQueryBuilder(ModuleLink.class);
		qb.eq("id", id).fetch("type");
		return (ModuleLink) queryFirst(qb);
	}

	@Override
	public List<ModuleLink> getModuleLinkList(int moduleLinkTypeId,int limit) {
		QueryBuilder<ModuleLink> qb = createQueryBuilder(ModuleLink.class);
		qb.eq("type.id", moduleLinkTypeId).OrderByAsc("order");
		if(limit==0){
			List<ModuleLink> list = queryListByQueryBuilder(qb);
			return list;
		}else{
			List<ModuleLink> list = queryTopByQueryBuilder(qb, limit);
			return list;
		}
	}

	@Override
	public void deleteModuleLinkById(Integer id) {
		ModuleLink link = get(id, ModuleLink.class);
		delete(link);
	}
}
